package com.spring.nio.core.event.listener;

import com.spring.nio.core.event.listener.event.FileResourceReleaseEvent;
import com.spring.nio.service.file.model.structure.FileTransportTaskDTO;
import com.spring.nio.util.LocalTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @Auther: YSFY
 * @Date: 2020-11-21 12:07
 * @Pacage_name: com.spring.nio.core.event.listener
 * @Project_Name: nio-server
 * @Description: 文件资源释放监听器
 */
@Slf4j
public class FileHandlerListener implements ApplicationListener<FileResourceReleaseEvent> {

    @Override
    public void onApplicationEvent(FileResourceReleaseEvent fileResourceReleaseEvent) {
        FileResourceReleaseEvent.FileEventSource fileEventSource = (FileResourceReleaseEvent.FileEventSource) fileResourceReleaseEvent.getSource();
        FileTransportTaskDTO fileTransportTaskDto = (FileTransportTaskDTO) fileEventSource.getObj();
        log.info("[ " + LocalTime.formatDate(LocalDateTime.now()) + " ] FileHandlerListener | --> 开始释放文件资源(关闭通道及删除文件), file = [{}]", fileTransportTaskDto.getFile().getName());

        int loop = 1;
        do {
            this.release(fileTransportTaskDto, loop);
            if(fileTransportTaskDto.getIsFileChannelClose() && fileTransportTaskDto.getIsFileDelete()) {
                break;
            }
            loop++;
        } while (loop < 6);
    }

    private void release(FileTransportTaskDTO fileTransportTaskDto, int loop) {
        // 关闭文件通道
        try {
            fileTransportTaskDto.getFileChannel().close();
            log.info("[ " + LocalTime.formatDate(LocalDateTime.now()) + " ] FileHandlerListener | --> 用户[{}]执行文件[{}]第[{}]次文件上传通道关闭成功",
                fileTransportTaskDto.getUserName(),
                fileTransportTaskDto.getFile().getName(),
                loop);
            fileTransportTaskDto.setIsFileChannelClose(Boolean.TRUE);
        } catch (IOException e) {
            log.info("[ " + LocalTime.formatDate(LocalDateTime.now()) + " ] FileHandlerListener | --> 用户[{}]执行文件[{}]第[{}]次文件上传通道关闭异常, error = {}",
                fileTransportTaskDto.getUserName(),
                fileTransportTaskDto.getFile().getName(),
                loop,
                e.getMessage());
            e.printStackTrace();
            fileTransportTaskDto.setIsFileChannelClose(Boolean.FALSE);
        } finally {
            // 删除上传文件
            File file = fileTransportTaskDto.getFile();
            if(file.exists()) {
                fileTransportTaskDto.setIsFileDelete(file.delete()?Boolean.TRUE:Boolean.FALSE);
                log.info("[ " + LocalTime.formatDate(LocalDateTime.now()) + " ] AbstractChannelHandler | --> 用户[{}]文件[{}]上传任务执行删除，结果 = [{}], 剩余任务数 = [{}] ",
                    fileTransportTaskDto.getUserName(),
                    fileTransportTaskDto.getFile().getName(),
                    fileTransportTaskDto.getIsFileDelete(),
                    fileTransportTaskDto.getRestTaskCount());
            }
        }
    }
}
